﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using SimpleLibraryClasses;

namespace SimpleLibraryWinforms
{
    public partial class SimpleLibraryForm : Form
    {
        public SimpleLibraryForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// The currently active member
        /// </summary>
        private Member LoadedMember { get; set; }

        /// <summary>
        /// The library object currently open
        /// </summary>
        private Library Library { get; set; }

        private void SimpleLibraryForm_Load(object sender, EventArgs e)
        {
            this.Library = Library.LoadLibrary("library.dat");
            ChooseMemberDialog memberDialog = new ChooseMemberDialog(this.Library.Members);
            memberDialog.ShowDialog();
            if (memberDialog.SelectedMember == null)
            {
                this.Close();
            }
            else
            {
                this.LoadedMember = memberDialog.SelectedMember;
            }
            DisplayLoans();
            DisplayBooks();
            DisplayDevices();
        }

        /// <summary>
        /// Refresh all library data views
        /// </summary>
        private void RefreshLibrary()
        {
            DisplayLoans();
            DisplayBooks();
            DisplayDevices();
        }

        /// <summary>
        /// Display the list of loans
        /// </summary>
        private void DisplayLoans()
        {
            loansListView.Items.Clear();
            foreach (Loan loan in this.LoadedMember.Loans)
            {
                ListViewItem item = new ListViewItem(loan.LoanItem.DisplayName);
                item.Tag = loan;
                item.SubItems.Add(loan.LoanItem.ItemCode);
                item.SubItems.Add(loan.LoanDate.ToShortDateString());
                item.SubItems.Add(loan.DueDate.ToShortDateString());
                loansListView.Items.Add(item);
            }
        }

        /// <summary>
        /// Display the list of Books
        /// </summary>
        private void DisplayBooks()
        {
            booksListView.Items.Clear();
            IEnumerable<Book> availableBooks = from book in Library.Items
                                               where book is Book && book.ItemStatus == ItemStatus.Available
                                               select book as Book;
            foreach (Book book in availableBooks)
            {
                ListViewItem item = new ListViewItem(book.Title);
                item.Tag = book;
                item.SubItems.Add(book.Author);
                item.SubItems.Add(book.Publisher);
                item.SubItems.Add(book.ItemCode);
                booksListView.Items.Add(item);
            }
        }

        /// <summary>
        /// Display the list of Devices
        /// </summary>
        private void DisplayDevices()
        {
            devicesListView.Items.Clear();
            IEnumerable<Device> availableDevices = from device in Library.Items
                                                   where device is Device && device.ItemStatus == ItemStatus.Available
                                                   select device as Device;
            foreach (Device device in availableDevices)
            {
                ListViewItem item = new ListViewItem(device.DeviceName);
                item.Tag = device;
                item.SubItems.Add(device.Model);
                item.SubItems.Add(device.Serial);
                item.SubItems.Add(device.DeviceType.ToString());
                devicesListView.Items.Add(item);
            }
        }

        /// <summary>
        /// Borrow all selected books
        /// </summary>
        private void borrowBookButton_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in booksListView.SelectedItems)
            {
                this.Library.BorrowItem(item.Tag as Item, this.LoadedMember);
            }
            this.RefreshLibrary();
        }

        /// <summary>
        /// Return all selected items to the library
        /// </summary>
        private void returnButton_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in loansListView.SelectedItems)
            {
                this.Library.ReturnItem(item.Tag as Loan);
            }
            this.RefreshLibrary();
        }

        /// <summary>
        /// Borrow all selected devices
        /// </summary>
        private void borrowDeviceButton_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in devicesListView.SelectedItems)
            {
                this.Library.BorrowItem(item.Tag as Item, this.LoadedMember);
            }
            this.RefreshLibrary();
        }
    }
}
